//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Matrix.h"
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Math::CMatrix::CMatrix (Cephei::QL::Math::IMatrix^ prm1) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CMatrix^ _Cprm1;
    try
    {
#ifdef HANDLE
        _phMatrix = NULL;
#endif
        _Cprm1 = safe_cast<CMatrix^> (prm1);
        _Cprm1->Lock();
        QuantLib::Matrix& _prm1 = static_cast<QuantLib::Matrix&> (_Cprm1->GetReference ()); 
        _ppMatrix = new boost::shared_ptr<QuantLib::Matrix> (new QuantLib::Matrix ( _prm1 ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
Cephei::QL::Math::CMatrix::CMatrix (UInt64 rows, UInt64 columns, Double value) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phMatrix = NULL;
#endif
        QuantLib::Size _rows = (QuantLib::Size)ValueHelper::Convert (rows);
        QuantLib::Size _columns = (QuantLib::Size)ValueHelper::Convert (columns);
        QuantLib::Real _value = (QuantLib::Real)ValueHelper::Convert (value);
        _ppMatrix = new boost::shared_ptr<QuantLib::Matrix> (new QuantLib::Matrix ( _rows,  _columns,  _value ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::CMatrix::CMatrix (UInt64 rows, UInt64 columns) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phMatrix = NULL;
#endif
        QuantLib::Size _rows = (QuantLib::Size)ValueHelper::Convert (rows);
        QuantLib::Size _columns = (QuantLib::Size)ValueHelper::Convert (columns);
        _ppMatrix = new boost::shared_ptr<QuantLib::Matrix> (new QuantLib::Matrix ( _rows,  _columns ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::CMatrix::CMatrix () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phMatrix = NULL;
#endif
        _ppMatrix = new boost::shared_ptr<QuantLib::Matrix> (new QuantLib::Matrix ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::CMatrix::CMatrix (boost::shared_ptr<QuantLib::Matrix>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMatrix = NULL;
#endif
	_ppMatrix = &childNative;
    
}
Cephei::QL::Math::CMatrix::CMatrix (QuantLib::Matrix& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMatrix = NULL;
#endif
	_ppMatrix = new boost::shared_ptr<QuantLib::Matrix> (&childNative);
    
    _MatrixOwner = owner;
    
}

Cephei::QL::Math::CMatrix::CMatrix (CMatrix^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMatrix = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMatrix = new boost::shared_ptr<QuantLib::Matrix> (copy->GetShared());
        
    }
}
Cephei::QL::Math::CMatrix::CMatrix (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMatrix = NULL;
#endif
	if (!t->IsSubclassOf(CMatrix::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CMatrix::CMatrix (QuantLib::Handle<QuantLib::Matrix>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMatrix = &childNative;
	_ppMatrix = &static_cast<boost::shared_ptr<QuantLib::Matrix>>(childNative.currentLink());
    
    _MatrixOwner = owner;
}
Cephei::QL::Math::CMatrix::CMatrix (QuantLib::Handle<QuantLib::Matrix> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMatrix = &childNative;
	_ppMatrix = &static_cast<boost::shared_ptr<QuantLib::Matrix>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CMatrix::CMatrix (QuantLib::Matrix childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMatrix = NULL;
#endif
	_ppMatrix = new boost::shared_ptr<QuantLib::Matrix> (new QuantLib::Matrix (childNative));
    
}
#endif

Cephei::QL::Math::CMatrix::~CMatrix ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMatrix != NULL)
    {
	    delete _ppMatrix;
        _ppMatrix = NULL;
    }
}
Cephei::QL::Math::CMatrix::!CMatrix ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMatrix != NULL)
    {
	    delete _ppMatrix;
    }
}
QuantLib::Matrix& Cephei::QL::Math::CMatrix::GetReference ()
{
    if (_ppMatrix == NULL) throw gcnew NativeNullException ();
	return **_ppMatrix;
}
boost::shared_ptr<QuantLib::Matrix>& Cephei::QL::Math::CMatrix::GetShared ()
{
    if (_ppMatrix == NULL) throw gcnew NativeNullException ();
	return *_ppMatrix;
}
QuantLib::Matrix* Cephei::QL::Math::CMatrix::GetPointer ()
{
    if (_ppMatrix == NULL) throw gcnew NativeNullException ();
	return &**_ppMatrix;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Matrix>& Cephei::QL::Math::CMatrix::GetHandle ()
{
	if (_phMatrix == NULL)
	{
		_phMatrix = new Handle<QuantLib::Matrix> (*_ppMatrix);
	}
	return *_phMatrix;
}
#endif
bool Cephei::QL::Math::CMatrix::HasNative () 
{
	return (_ppMatrix != NULL);
}

Double Cephei::QL::Math::CMatrix::At (UInt64 prm1)
{
    try
    {
        QuantLib::Size _prm1 = (QuantLib::Size)ValueHelper::Convert (prm1);
    	const QuantLib::Real* _rv = (const QuantLib::Real*)(*_ppMatrix)->at ( _prm1 );   
        Double _nrv = (Double)*_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CMatrix::Begin::get ()
{
    try
    {
    	const QuantLib::Real* _rv = (const QuantLib::Real*)(*_ppMatrix)->begin ( );   
        Double _nrv = (Double)*_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::CMatrix::Columns::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMatrix)->columns ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Math::CMatrix::Empty::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppMatrix)->empty ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CMatrix::End::get ()
{
    try
    {
    	const QuantLib::Real* _rv = (const QuantLib::Real*)(*_ppMatrix)->end ( );   
        Double _nrv = (Double)*_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CMatrix::Row_begin (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	const QuantLib::Real* _rv = (const QuantLib::Real*)(*_ppMatrix)->row_begin ( _i );   
        Double _nrv = (Double)*_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CMatrix::Row_end (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	const QuantLib::Real* _rv = (const QuantLib::Real*)(*_ppMatrix)->row_end ( _i );   
        Double _nrv = (Double)*_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::CMatrix::Rows::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMatrix)->rows ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Math::CMatrix::Swap (Cephei::QL::Math::IMatrix^ prm1)
{
    CMatrix^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CMatrix^> (prm1);
        _Cprm1->Lock();
        QuantLib::Matrix& _prm1 = static_cast<QuantLib::Matrix&> (_Cprm1->GetReference ()); 
    	(*_ppMatrix)->swap ( _prm1 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
Cephei::QL::Math::CMatrixVector::CMatrixVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::Math::CMatrixVector::~CMatrixVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::Math::CMatrixVector::!CMatrixVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::Math::CMatrixVector::CMatrixVector (std::vector<QuantLib::Matrix>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::Math::CMatrixVector::CMatrixVector (std::vector<QuantLib::Matrix> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::Matrix> (vec);
}
#endif
Cephei::QL::Math::CMatrixVector::CMatrixVector (std::vector<boost::shared_ptr<QuantLib::Matrix>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::Math::CMatrixVector::CMatrixVector (std::vector<QuantLib::Handle<QuantLib::Matrix>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::Math::CMatrixVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Math::CMatrixVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw gcnew RangeException ();
		QuantLib::Matrix& native = (*_pVValue)[index];
		return gcnew CMatrix (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw gcnew RangeException ();
		QuantLib::Matrix& native = *(*_pVBoost)[index];
		return gcnew CMatrix (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw gcnew RangeException ();
		QuantLib::Matrix& native = *((*_pVHand)[index].currentLink ());
		return gcnew CMatrix (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::Math::CMatrixVector::default::set (int index, Cephei::QL::Math::IMatrix^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CMatrix^>(value)->GetReference ());
		}
        else
        {
            QuantLib::Matrix& c = (*_pVValue)[index];
            c = dynamic_cast<CMatrix^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CMatrix^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::Matrix>& c = (*_pVBoost)[index];
		c = dynamic_cast<CMatrix^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::Matrix> ());
		}
		QuantLib::Handle<QuantLib::Matrix>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::Matrix>& b = static_cast<boost::shared_ptr<QuantLib::Matrix>>(c.currentLink());
		b = dynamic_cast<CMatrix^>(value)->GetShared ();
	}
#endif
}
Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^ Cephei::QL::Math::CMatrixVector::CItem::get (int index) 
{
	return CellBuilder::Instance->Create<IMatrix^> (dynamic_cast<IMatrix^>(default[index]));
}

void Cephei::QL::Math::CMatrixVector::CItem::set (int index, Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^ value) 
{
	default[index] = safe_cast<IMatrix^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::Matrix>& Cephei::QL::Math::CMatrixVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::Matrix>>& Cephei::QL::Math::CMatrixVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::Matrix>>& Cephei::QL::Math::CMatrixVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::Math::CMatrixVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::Matrix> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::Matrix>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::Matrix>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
Cephei::QL::Math::CMatrixMatrix::CMatrixMatrix ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif    
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
#ifdef STRUCT
Cephei::QL::Math::CMatrixMatrix::CMatrixMatrix (std::vector<std::vector<QuantLib::Matrix>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif    
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::Math::CMatrixMatrix::CMatrixMatrix (std::vector<std::vector<QuantLib::Matrix>> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif    
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<std::vector<QuantLib::Matrix>> (vec);
}
#endif
Cephei::QL::Math::CMatrixMatrix::CMatrixMatrix (std::vector<std::vector<boost::shared_ptr<QuantLib::Matrix>>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif    
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::Math::CMatrixMatrix::CMatrixMatrix (std::vector<std::vector<QuantLib::Handle<QuantLib::Matrix>>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif    
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVHand = &vec;
	_owner = owner;
}
#endif
Cephei::QL::Math::CMatrixMatrix::~CMatrixMatrix ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::Math::CMatrixMatrix::!CMatrixMatrix ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
int Cephei::QL::Math::CMatrixMatrix::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Math::CMatrixMatrix::default::get (int x, int y) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		if ((size_t)x >= _pVValue->size() || (size_t)y >= (*_pVValue)[x].size()) throw gcnew RangeException ();
		QuantLib::Matrix& native = (*_pVValue)[x][y];
		return gcnew CMatrix (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
		if ((size_t)x >= _pVBoost->size() || (size_t)y >= (*_pVBoost)[x].size()) throw gcnew RangeException ();
		QuantLib::Matrix& native = *(*_pVBoost)[x][y];
		return gcnew CMatrix (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if ((size_t)x >= _pVHand->size() || (size_t)y >= (*_pVHand)[x].size()) throw gcnew RangeException ();
		QuantLib::Matrix& native = *((*_pVHand)[x][y].currentLink ());
		return gcnew CMatrix (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::Math::CMatrixMatrix::default::set (int x, int y, Cephei::QL::Math::IMatrix^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)x)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size()-1 < (size_t)x) throw gcnew RangeException ();

			_pVValue->push_back (std::vector<QuantLib::Matrix>());
		}
		if ((*_pVValue)[x].size() <= (size_t)y)
		{
			if ((*_pVValue)[x].size() < (size_t)y) throw gcnew RangeException ();

			(*_pVValue)[x].push_back (dynamic_cast<CMatrix^>(value)->GetReference ());
		}
        else
        {    
            QuantLib::Matrix& c = (*_pVValue)[x][y];
            c = dynamic_cast<CMatrix^>(value)->GetReference ();
        }
	}
#endif    
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)x)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size()-1 < (size_t)x) throw gcnew RangeException ();

			_pVBoost->push_back (std::vector<boost::shared_ptr<QuantLib::Matrix>>());
		}
		if ((*_pVBoost)[x].size() <= (size_t)y)
		{
			if ((*_pVBoost)[x].size() < (size_t)y) throw gcnew RangeException ();

			(*_pVBoost)[x].push_back (dynamic_cast<CMatrix^>(value)->GetShared ());
		}
        else
        {
            boost::shared_ptr<QuantLib::Matrix>& c = (*_pVBoost)[x][y];
            c = dynamic_cast<CMatrix^>(value)->GetShared ();
        }
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)x)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size()-1 < (size_t)x) throw gcnew RangeException ();

			_pVHand->push_back (std::vector<QuantLib::Handle<QuantLib::Matrix>>());
		}
		if ((*_pVHand)[x].size() <= (size_t)y)
		{
			if ((*_pVHand)[x].size() < (size_t)y) throw gcnew RangeException ();

			(*_pVHand)[x].push_back (QuantLib::Handle<QuantLib::Matrix> ());
		}
		QuantLib::Handle<QuantLib::Matrix>& c = (*_pVHand)[x][y];
		boost::shared_ptr<QuantLib::Matrix>& b = static_cast<boost::shared_ptr<QuantLib::Matrix>>(c.currentLink());
		b = dynamic_cast<CMatrix^>(value)->GetShared ();
	}
#endif
}
Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^ Cephei::QL::Math::CMatrixMatrix::CItem::get (int x, int y) 
{
	return CellBuilder::Instance->Create<Cephei::QL::Math::IMatrix^> (dynamic_cast<IMatrix^>(default[x,y]));
}
void Cephei::QL::Math::CMatrixMatrix::CItem::set (int x, int y, Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^ value)
{
	default[x,y] = safe_cast<IMatrix^>(value->Value);
}

#ifdef STRUCT
std::vector<std::vector<QuantLib::Matrix>>& Cephei::QL::Math::CMatrixMatrix::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<std::vector<boost::shared_ptr<QuantLib::Matrix>>>& Cephei::QL::Math::CMatrixMatrix::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<std::vector<QuantLib::Handle<QuantLib::Matrix>>>& Cephei::QL::Math::CMatrixMatrix::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::Math::CMatrixMatrix::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<std::vector<QuantLib::Matrix>> ();
				return true;
			}
			break;
#endif            
		case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<std::vector<boost::shared_ptr<QuantLib::Matrix>>> ();
				return true;
			}
			break;
#ifdef HANDLE
		case NativeFeature::Handle :
			if (_pVHand == NULL)
			{
				_pVHand = new std::vector<std::vector<QuantLib::Handle<QuantLib::Matrix>>> ();
				return true;
			}
			break;
#endif
	}
	return false;
}
INativeVector<Cephei::QL::Math::IMatrix^>^ Cephei::QL::Math::CMatrixMatrix::CreateVector () 
{
	return gcnew CMatrixVector ();
}
INativeVector<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^>^ Cephei::QL::Math::CMatrixMatrix::CreateCellVector ()
{
	return gcnew CMatrixVector ();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::IMatrix^ Cephei::QL::Math::CMatrix_Factory::Create (Cephei::QL::Math::IMatrix^ prm1)
{
    return gcnew CMatrix ( prm1);
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Math::CMatrix_Factory::Create (UInt64 rows, UInt64 columns, Double value)
{
    return gcnew CMatrix ( rows,  columns,  value);
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Math::CMatrix_Factory::Create (UInt64 rows, UInt64 columns)
{
    return gcnew CMatrix ( rows,  columns);
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Math::CMatrix_Factory::Create ()
{
    return gcnew CMatrix ();
}
/* <summary>Create a single dimension array of Matrix that can be passed into other function </summary> */
IVector<Cephei::QL::Math::IMatrix^>^ Cephei::QL::Math::CMatrix_Factory::CreateVector ()
 {
	 return gcnew CoVector<IMatrix^> (gcnew CMatrixVector ());
 }
IVector<Cephei::QL::Math::IMatrix^>^ Cephei::QL::Math::CMatrix_Factory::CreateVector (System::Collections::Generic::IEnumerable<IMatrix^>^ source)
 {
	 return gcnew CoVector<IMatrix^> (source, gcnew CMatrixVector ());
 }
/* <summary>Create a single dimension array of Matrix with change events</summary> */
Cephei::Generic::ICell<IVector<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^>^>^ Cephei::QL::Math::CMatrix_Factory::CreateCellVector ()
 {
	 return CellBuilder::Instance->Create<IVector<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^>^> (gcnew CoCellVector<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^> (gcnew CMatrixVector ()));
 }
Cephei::Generic::ICell<IVector<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^>^>^ Cephei::QL::Math::CMatrix_Factory::CreateCellVector (System::Collections::Generic::IEnumerable<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^>^ source)
 {
	 return CellBuilder::Instance->Create<IVector<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^>^> (gcnew CoCellVector<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^> (source, gcnew CMatrixVector ()));
 }

/* <summary> Create a two dimension array of Matrix that can be passed into other function </summary> */
Cephei::IMatrix<Cephei::QL::Math::IMatrix^>^ Cephei::QL::Math::CMatrix_Factory::CreateMatrix ()
 {
	 return gcnew CoMatrix<IMatrix^> (gcnew CMatrixMatrix ());
 }
Cephei::IMatrix<Cephei::QL::Math::IMatrix^>^ Cephei::QL::Math::CMatrix_Factory::CreateMatrix (System::Collections::Generic::IEnumerable<IVector<IMatrix^>^>^ source)
 {
	 return gcnew CoMatrix<IMatrix^> (source, gcnew CMatrixMatrix ());
 }
/* <summary> Create a two dimension array of Matrix with change events </summary> */
Cephei::Generic::ICell<Cephei::IMatrix<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^>^>^ Cephei::QL::Math::CMatrix_Factory::CreateCellMatrix ()
 {
	 return CellBuilder::Instance->Create<Cephei::IMatrix<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^>^> (gcnew CoCellMatrix<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^> (gcnew CMatrixMatrix ()));
 }
Cephei::Generic::ICell<Cephei::IMatrix<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^>^>^ Cephei::QL::Math::CMatrix_Factory::CreateCellMatrix (System::Collections::Generic::IEnumerable<IVector<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^>^>^ source)
 {
	 return CellBuilder::Instance->Create<Cephei::IMatrix<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^>^> (gcnew CoCellMatrix<Cephei::Generic::ICell<Cephei::QL::Math::IMatrix^>^> (source, gcnew CMatrixMatrix ()));
 }

